package com.maila.xiaoaoschool.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.adapter.CommentAdapter;
import com.maila.xiaoaoschool.adapter.ShopDetailsAdapter;
import com.maila.xiaoaoschool.adapter.ShopMunuAdapter;
import com.maila.xiaoaoschool.entity.FoodEntity;
import com.maila.xiaoaoschool.util.StartActivityUtils;
import com.maila.xiaoaoschool.widget.PinnedHeaderListView;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/9/26.
 */

public class ShopDetailsActivity extends AppCompatActivity {
    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.tv_shop_namee)
    TextView tvShopName;
    @BindView(R.id.tv_good)
    TextView tvGood;
    @BindView(R.id.v_good)
    View vGood;
    @BindView(R.id.rl_good)
    AutoRelativeLayout rlGood;
    @BindView(R.id.tv_comment)
    TextView tvComment;
    @BindView(R.id.v_comment)
    View vComment;
    @BindView(R.id.rl_comment)
    AutoRelativeLayout rlComment;
    @BindView(R.id.ll_title)
    AutoLinearLayout llTitle;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.ll_gonggao)
    AutoLinearLayout llGonggao;
    @BindView(R.id.lv_comment)
    ListView lvComment;
    @BindView(R.id.lv_menu)
    ListView lvMenu;
    @BindView(R.id.pinnedListView)
    PinnedHeaderListView pinnedListView;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_fee)
    TextView tvFee;
    @BindView(R.id.btn_commit)
    TextView btnCommit;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.rl_gouwuche)
    AutoRelativeLayout rlGouwuche;
    @BindView(R.id.ll_shop_details)
    AutoRelativeLayout llShopDetails;
    @BindView(R.id.rel_layout)
    AutoRelativeLayout relLayout;
    List<FoodEntity.category> categories=new ArrayList<>();
    private HashMap<Integer,Integer> location=new HashMap<>();
    ShopMunuAdapter meumadapter;
    ShopDetailsAdapter sdadapter;
    private int mSection=0;
    int showtype=0;
    CommentAdapter commentAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_details);
        ButterKnife.bind(this);
        meumadapter=new ShopMunuAdapter(this);
        sdadapter=new ShopDetailsAdapter(this);
        commentAdapter=new CommentAdapter(this);
        lvComment.setAdapter(commentAdapter);
        lvMenu.setAdapter(meumadapter);
        pinnedListView.setAdapter(sdadapter);
        getData();
        getView();
        setmenustatus();
    }

    //获取数据
    private void getData() {
        List<FoodEntity.category.foodbean> foodbeanList;
        int num = 0;
        for (int i = 0; i < 8; i++) { //三种类型
            num++;
            FoodEntity.category category = new FoodEntity.category();
            category.setName("类型" + i);
            foodbeanList = new ArrayList<>();
            int random = new Random().nextInt(6) + 3;//随机
            for (int j = 0; j < random; j++) {
                num++;
                FoodEntity.category.foodbean foodbean = new FoodEntity.category.foodbean();
                foodbean.setDishes("鲍鱼" + j);
                foodbeanList.add(foodbean);
            }
            category.setFoodbeans(foodbeanList);
            categories.add(category);
            location.put(i, num);
            meumadapter.setMdata(categories);
            sdadapter.setFoodbeanList(categories);
        }
    }
    @OnClick({R.id.white_return_iv, R.id.rl_good, R.id.rl_comment, R.id.btn_commit, R.id.rl_gouwuche})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.white_return_iv:
                finish();
                break;
            case R.id.rl_good:
                showtype=0;
                setmenustatus();
                break;
            case R.id.rl_comment:
                showtype=1;
                setmenustatus();
                break;
            case R.id.btn_commit:
                StartActivityUtils.start(this, AffirmOrderActivity.class);
                break;
            case R.id.rl_gouwuche:
                break;
        }
    }

    private void setmenustatus() {
        if(showtype==0){
            tvComment.setTextColor(Color.parseColor("#666666"));
            vComment.setVisibility(View.GONE);
            vGood.setVisibility(View.VISIBLE);
            tvGood.setTextColor(Color.parseColor("#f2673e"));
            lvComment.setVisibility(View.GONE);
            llShopDetails.setVisibility(View.VISIBLE);
        }else {
            tvComment.setTextColor(Color.parseColor("#f2673e"));
            vComment.setVisibility(View.VISIBLE);
            vGood.setVisibility(View.GONE);
            tvGood.setTextColor(Color.parseColor("#666666"));
            lvComment.setVisibility(View.VISIBLE);
            llShopDetails.setVisibility(View.GONE);
        }

    }

    public void getView() {
        pinnedListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                int section = sdadapter.getSectionForPosition(firstVisibleItem);//获取当前的head
                if (mSection != section) {
                    meumadapter.setIndex(section);
                    mSection = section;
                }
            }
        });
        pinnedListView.setOnItemClickListener(new PinnedHeaderListView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int section, int position, long id) {
                StartActivityUtils.start(ShopDetailsActivity.this,FoodDetailActivity.class);
            }

            @Override
            public void onSectionClick(AdapterView<?> adapterView, View view, int section, long id) {

            }
        });
        lvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    pinnedListView.setSelection(0);
                } else {
                    pinnedListView.setSelection(location.get(position - 1));

                }
                meumadapter.setIndex(position);
            }
        });

    }

}
